/**
 * Created by Administrator on 2017/5/31.
 */
/*
 * @Author: yfl
 * @Date:   2016-12-15 17:20:54
 * @Last Modified by:   yfl
 * @Last Modified time: 2016-12-24 21:51:17
 * +----------------------------------------------------------------------
 * | Author: 554665488@qq.com <554665488@qq.com>
 * +----------------------------------------------------------------------
 */
'use strict';

layui.use(['jquery'], function () {
    window.jQuery = window.$ = layui.jquery;
    //获取窗口宽高并设置
    function setWindow() {
        var winW = $(window).width();
        var winH = $(window).height();

        $(".layui-canvs").width(winW);
        $(".layui-canvs").height(winH);
    }

    //当窗口改变时重新获取
    $(window).on('resize', function () {
        setWindow();
    });
    setWindow();
    //提交登录
    $('.submit_btn').click(function () {

        var name = $("#user_name").val();
        var pwd = $("#password").val();
        var captcha = $("#captcha").val();
        if (name.length == 0) {

            layer.tips('请输入用户名', '#user_name');
            return;
        }

        if (pwd.length == 0) {
            layer.tips('请输入密码', '#password');
            return;
        }
        if (captcha.length == 0) {
            layer.tips('请输入验证码', '#captcha');
            return;
        }
        // if (captcha.length == 0) {
        //     layer.tips('请输入验证码', '#captcha');
        //     return;
        // }
        var url = _Admin + 'Login/doLogin';
        $.post(url, {user_name: name, password: pwd, captcha: captcha}, function (data) {
            layer.load(1, {time: 3 * 1000}); //又换了种风格，并且设定最长等待5秒
            if (data.status) {
                // $('#code_img').click();
                // $('#verifyImg').trigger('click');
                layer.msg(data.msg);
                setTimeout(function () {
                    layer.closeAll('loading');
                }, 2000);
            } else {
                if (data.user_level == "0") {
                    window.location.href = _APP + 'Index/index';
                } else {
                    // window.location.href= _APP+'Index/index';
                    window.location.href = _APP + 'news/index/m/2';
                }
                //

            }
        })
    });
    $(window).keyup(function (event) {
        if (event.keyCode == 13) {
            var name = $("#user_name").val();
            var pwd = $("#password").val();
            var captcha = $("#captcha").val();
            // var captcha = $("#captcha").val();
            if (name.length == 0) {

                layer.tips('请输入用户名', '#user_name');
                return;
            }

            if (pwd.length == 0) {
                layer.tips('请输入密码', '#password');
                return;
            }
            if (captcha.length == 0) {
                layer.tips('请输入验证码', '#captcha');
                return;
            }
            // if (captcha.length == 0) {
            //     layer.tips('请输入验证码', '#captcha');
            //     return;
            // }
            var url = _APP + 'Login/doLogin';
            $.post(url, {user_name: name, password: pwd, captcha: captcha}, function (data) {
                layer.load(1, {time: 3 * 1000}); //又换了种风格，并且设定最长等待5秒
                if (data.status) {
                    // $('#code_img').click();
                    // $('#verifyImg').trigger('click');
                    layer.msg(data.msg);
                } else {
                    if (data.user_level == "0") {
                        window.location.href = _APP + 'Index/index';
                    } else {
                        window.location.href = _APP + 'news/index/m/2';
                    }
                }
            })
        }

    });

    //获取手机验证码
    var getCode = function () {
        var name = $("#user_name").val();
        var pwd = $("#password").val();
        if (name.length == 0) {
            layer.tips('请输入用户名', '#user_name');
            return;
        }

        if (pwd.length == 0) {
            layer.tips('请输入密码', '#password');
            return;
        }
        var url = _APP + 'Login/getUserPhone';
        $.ajax({
            url: url,
            dataType: 'json',
            type: 'post',
            data: {name: name, pwd: pwd},
            cache: false,
            async: true,
            beforeSend: function (XMLHttpRequest) {
                layer.load(2, {time: 3 * 1000}); //又换了种风格，并且设定最长等待10秒
            },
            complete: function (XMLHttpRequest) { //请求完成时运行的函数（在请求成功或失败之后均调用，即在 success 和 error 函数之后）。
                layer.closeAll('loading');
            },
            success: function (json) {
                if (json.status == 1) {
                    layer.msg(json.msg);
                    return false;
                }
                var regex = /^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$/;
                var phone = json.phone;
                if (phone.match(regex) == null) {
                    layer.msg('手机号不正确', {icon: 5});
                    return false;
                }
                sendCode(phone);  //发送验证码
                return false;
            },
            error: function () {
                layer.msg('请求失败.或者没有权限。', {icon: 2})
            }
        });
    };

    $('.get_code').on('click', getCode);
    //发送验证码  "{:U('AuthGroup/addChildGroup')}"
    function sendCode(phone) {
        var is_status = false;
        var wait = 60;
        var t_img;
        $.ajax({
            url: sendUrl,
            dataType: 'json',
            type: 'post',
            data: {phone: phone},
            cache: false,
            async: true,
            success: function (json) {
                if (json.status == 1) {  //no-click

                    $('.get_code').addClass('no-click').attr('style', 'background:#686D6F');
                    time_run()
                } else {
                    layer.msg(json.info, {icon: 5});
                }
            },
            error: function () {
            }
        });
        function time_run() {
            var obj = $('.get_code');
            if (wait == 0) {
                is_status = true;
            }
            if (is_status) {
                clearTimeout(t_img); // 清除定时器
                obj.on('click', getCode);  //绑定click
                obj.removeClass('no-click').attr('style', 'background:#5ab5e7');
                $('#time').empty();
            } else {
                is_status = false;
                obj.off('click');  //移除click
                $('#time').html(wait);
                wait--;
                t_img = setTimeout(function () {
                    time_run();
                }, 1000);
            }
        }
    }

});
